﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace WodLib.Tools
{
    public static class TraitsCalculator
    {
        public enum LengthUnit
        {
            Meter,
            Yard,
            Mile
        }

        public enum SpeedUnit
        {
            MetersPerSecond,
            KilometersPerHour,
            MilesPerHour,
            YardsPerTurn
        }

        private const double YARDINMETERS = 3600.0 / 3937.0;

        private const double MILEINMETERS = 1609.344;

        private const double KMPH_IN_MPS = 1000.0 / 3600.0;

        //yps = m/3s

        private static double YardsToMeters(double lengthInYards)
        {
            return lengthInYards * YARDINMETERS;
        }

        private static double MetersToYards(double lengthInMeters)
        {
            return lengthInMeters / YARDINMETERS;
        }

        private static double MetersToMiles(double lengthInMeters)
        {
            return lengthInMeters / MILEINMETERS;
        }

        private static double MilesToMeters(double lengthInMiles)
        {
            return lengthInMiles * MILEINMETERS;
        }

        public static double ConvertLength(double length, LengthUnit inputUnit, LengthUnit outputUnit)
        {
            if (inputUnit == outputUnit)
                return length;

            double lengthInMeters = 0.0;

            switch (inputUnit)
            {
                case LengthUnit.Yard:
                    lengthInMeters = YardsToMeters(length);
                    break;
                case LengthUnit.Meter:
                    lengthInMeters = length;
                    break;
                case LengthUnit.Mile:
                    lengthInMeters = MilesToMeters(length);
                    break;
                default:
                    break;
            }

            switch (outputUnit)
            {
                case LengthUnit.Yard:
                    return MetersToYards(lengthInMeters);
                case LengthUnit.Meter:
                    return lengthInMeters;
                case LengthUnit.Mile:
                    return MetersToMiles(lengthInMeters);
                default:
                    break;
            }

            return 0;
        }

        private static double KilometersPerHourToMetersPerSecond(double speed)
        {
            return speed * KMPH_IN_MPS;
        }

        private static double MetersPerSecondToKilometersPerHour(double speed)
        {
            return speed / KMPH_IN_MPS;
        }

        private static double YardsPerTurnToMetersPerSecond(double speed)
        {
            speed = speed / 3; //turn is 3 seconds
            speed = YardsToMeters(speed);
            return speed;
        }

        private static double MetersPerSecondToYardsPerTurn(double speed)
        {
            speed = speed * 3; // turn is 3 seconds
            return MetersToYards(speed);
        }

        private static double MilesPerHourToMetersPerSecond(double speed)
        {
            speed = MilesToMeters(speed); // convert to meters per hour
            return speed / 3600; //to meters per second
        }

        private static double MetersPerSecondToMilesPerHour(double speed)
        {
            speed = MetersToMiles(speed); // convert to miles per second
            return speed * 3600; // to miles per second
        }

        public static double ConvertSpeed(double speed, SpeedUnit inputUnit, SpeedUnit outputUnit)
        {
            double speedInMPS= 0.0;
            switch (inputUnit)
            {
                case SpeedUnit.KilometersPerHour:
                    speedInMPS = KilometersPerHourToMetersPerSecond(speed);
                    break;
                case SpeedUnit.MilesPerHour:
                    speedInMPS = MilesPerHourToMetersPerSecond(speed);
                    break;
                case SpeedUnit.MetersPerSecond:
                    speedInMPS = speed;
                    break;
                case SpeedUnit.YardsPerTurn:
                    speedInMPS = YardsPerTurnToMetersPerSecond(speed);
                    break;
                default:
                    break;
            }

            switch (outputUnit)
            {
                case SpeedUnit.KilometersPerHour:
                    return MetersPerSecondToKilometersPerHour(speedInMPS);
                case SpeedUnit.MilesPerHour:
                    return MetersPerSecondToMilesPerHour(speedInMPS);
                case SpeedUnit.MetersPerSecond:
                    return speedInMPS;
                case SpeedUnit.YardsPerTurn:
                    return MetersPerSecondToYardsPerTurn(speedInMPS);
                default:
                    break;
            }

            return 0.0;
        }


    }
}
